const { Random } = require("mockjs");
const baseUrl = "/api/question";
const getComponentList = require("./data/getComponentList");
const getQuestionList = require("./data/getQuestionList");
const getStatList = require("./data/getStatList");

module.exports = [
  // 根据id查询问卷
  {
    url: `${baseUrl}/:id`,
    method: "get",
    response() {
      return {
        code: 0,
        result: {
          id: Random.id(),
          title: Random.ctitle(),
          desc: Random.cparagraph(),
          css: "",
          js: "",
          isDeleted: false,
          isPublished: true,
          componentList: getComponentList(),
        },
      };
    },
  },
  // 创建问卷
  {
    url: baseUrl,
    method: "post",
    response() {
      return {
        code: 0,
        result: {
          id: Random.id(),
        },
      };
    },
  },
  // 查询问卷列表
  {
    url: `${baseUrl}/list`,
    method: "get",
    response(ctx) {
      const { url, query = {} } = ctx;
      const pageSize = parseInt(query.pageSize);
      const isStar = url.indexOf("isStar=true") > -1;
      const isDeleted = url.indexOf("isDeleted=true") > -1;
      return {
        code: 0,
        result: {
          list: getQuestionList({
            len: pageSize ? pageSize : Random.natural(0, 10),
            isStar,
            isDeleted,
          }),
          total: 100,
        },
      };
    },
  },
  // 更新问卷
  {
    url: `${baseUrl}/:id`,
    method: "patch",
    response() {
      return {
        code: 0,
      };
    },
  },
  // 复制问卷
  {
    url: `${baseUrl}/duplicate/:id`,
    method: "post",
    response() {
      return {
        code: 0,
        result: { id: Random.id() },
      };
    },
  },
  // 删除问卷
  {
    url: baseUrl,
    method: "delete",
    response() {
      return {
        code: 0,
      };
    },
  },
  // 获取统计列表
  {
    url: `${baseUrl}/stat/:questionId`,
    method: "get",
    response(ctx) {
      const pageSize = ctx.request.query.pageSize;

      return {
        code: 0,
        result: {
          total: 100,
          list: getStatList(pageSize),
        },
      };
    },
  },
  // 获取单个组件统计信息
  {
    url: `${baseUrl}/stat/:questionId/:componentId`,
    method: "get",
    response() {
      return {
        code: 0,
        result: {
          stat: [
            {
              name: "选项1",
              count: 20,
            },
            {
              name: "选项2",
              count: 30,
            },
            {
              name: "选项3",
              count: 50,
            },
          ],
        },
      };
    },
  },
];
